<?php

class AppModel extends Model
{
	const ENUM_YES = 'Yes';

	public function bindModels($relations, $reset = true)
	{
        foreach ($relations as $relation)
        {
            if (isset($this->binds[$relation]))
            {
                $this->bindModel($this->binds[$relation], $reset);
            }
        }
    }

    /**
     * Save data in common case
     *
     * @param array $data
     * @param string $uniqueKey
     *
     * @return array
     * 			status 0|1
     * 			message error if has
     */
    public function saveIt(&$data, $uniqueKey = null){
    	$model = $this->name;
    	if(null !== $uniqueKey){
			$id = $this->field($this->primaryKey, array(
				$uniqueKey => $data[$model][$uniqueKey]
			));
    	} else {
			$id = isset($data[$model][$this->primaryKey]) ? $data[$model][$this->primaryKey] : null;
    	}
    	if(!$id){
    		$this->create();
    	}
    	$this->set($data);
    	if($this->validates()){
    		$this->save();
    		$data[$model][$this->primaryKey] = $this->getLastInsertId();
    		return array(
    			'status' => 0,
    			'message' => '',
    		);
    	} else {
    		$data[$model][$this->primaryKey] = $id;
			return array(
				'status' => 1,
				'message' => implode('<br/>', $this->validationErrors)
			);
    	}
    }
}